﻿using XEvolve.Presentation.Input.Internal;
using System.Windows;

namespace XEvolve.Presentation.Input
{
    public static class CommandBindings
    {
        internal static XCommandManager Manager;

        public static readonly DependencyProperty ContextProperty = DependencyProperty.RegisterAttached("Context",
                                                                                                        typeof (string),
                                                                                                        typeof (CommandBindings),
                                                                                                        new FrameworkPropertyMetadata
                                                                                                            (string.Empty, OnContextChanged));

        public static void SetContext(UIElement element, string value)
        {
            element.SetValue(ContextProperty, value);
        }

        public static string GetContext(UIElement element)
        {
            return (string) element.GetValue(ContextProperty);
        }

        private static void OnContextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var target = (UIElement) d;
            var oldContactIds = (string)e.OldValue;
            foreach (var context in oldContactIds.Split(','))
            {
                var bindings = Manager.GetCommandBindings(context);
                foreach (var binding in bindings)
                {
                    target.CommandBindings.Remove(binding);
                }
            }

            var contextIds = (string) e.NewValue;
            foreach (var context in contextIds.Split(','))
            {
                var bindings = Manager.GetCommandBindings(context);
                target.CommandBindings.AddRange(bindings);
            }
        }
    }

    public static class Elements
    {
        internal static AdvisorManager Manager;

        public static readonly DependencyProperty IdProperty = DependencyProperty.RegisterAttached("Id", typeof (string),
                                                                                                   typeof (Elements),
                                                                                                   new FrameworkPropertyMetadata
                                                                                                       (string.Empty,
                                                                                                        OnElementIdChanged));

        public static void SetId(UIElement element, string value)
        {
            element.SetValue(IdProperty, value);
        }

        private static void OnElementIdChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if ((string)e.OldValue != string.Empty) return;
            foreach (var advisor in Manager.GetAdvisors((string) e.NewValue))
            {
                advisor.Initialize(d);
            }
        }
    }
}